package com.bitdf.txing.chat.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.TypeReference;
import com.bitdf.txing.chat.entity.AddFriendRequestEntity;
import com.bitdf.txing.chat.feign.ChatUserFeignService;
import com.bitdf.txing.chat.service.AddFriendRequestService;
import com.bitdf.txing.chat.to.AddFriendRequestReplyTo;
import com.bitdf.txing.common.enume.TxCodeEnume;
import com.bitdf.txing.common.to.UserTo;
import com.bitdf.txing.common.utils.PageUtils;
import com.bitdf.txing.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author lzw
 * @email 123@qq.com
 * @date 2022-11-23 22:43:50
 */
@RestController
@RequestMapping("${txing.request.path.chat}/friend")
public class AddFriendRequestController {


    @Autowired
    private AddFriendRequestService addFriendRequestService;
    @Autowired
    private ChatUserFeignService txUserFeignService;
    /**
     * 请求加好友
     *
     * @param friendId
     * @return
     */
    @GetMapping("/request")
    @SaCheckLogin
    public R requestAddFriend(@RequestParam("friendId") Long friendId) {
        Long selfId = Long.parseLong((String) StpUtil.getLoginId());
        AddFriendRequestEntity entity = addFriendRequestService.requestAddFriend(selfId, friendId);
        if (entity == null) {
            return R.error(TxCodeEnume.COMMON_OPS_FAILURE_EXCEPTION);
        }

        //返回必要信息供前端显示
        R r = txUserFeignService.getUserById(entity.getReceiveUserId());
        UserTo receiveUser = r.getData(new TypeReference<UserTo>() {
        });
        R r1 = txUserFeignService.getUserById(entity.getSendUserId());
        UserTo sendUser = r1.getData(new TypeReference<UserTo>() {
        });

        AddFriendRequestReplyTo replyTo = new AddFriendRequestReplyTo(entity, sendUser, receiveUser);

        return R.ok().put("data", replyTo);
        //TODO
    }

    /**
     * 获取发送的添加好友消息
     *
     * @return
     */
    @GetMapping("/requests/get")
    @SaCheckLogin
    public R getAddFriendRequest() {
        Long selfId = Long.parseLong((String) StpUtil.getLoginId());
        List<AddFriendRequestEntity> list = addFriendRequestService.getAddFriendRequest(selfId);
        return R.ok().put("data", list);
    }

    /**
     * 同意好友请求
     *
     * @param id
     * @return
     */
    @SaCheckLogin
    @GetMapping("/request/agree")
    public R agreeAddRequest(@RequestParam("id") Long id) {
        Long selfId = Long.parseLong((String) StpUtil.getLoginId());
        AddFriendRequestEntity entity = addFriendRequestService.agreeAddFriendRuquest(id, selfId);
        if (entity == null) {
            return R.error(TxCodeEnume.COMMON_SYSTEM_UNKNOWN_EXCEPTION);
        }

        // 保持新好友和自己，用于更新自己的UI和好友的UI
        R r1 = txUserFeignService.getUserById(selfId);
        UserTo receiveUser = r1.getData(new TypeReference<UserTo>() {
        });
        R r2 = txUserFeignService.getUserById(entity.getSendUserId());
        UserTo sendUser = r2.getData(new TypeReference<UserTo>() {
        });

        return R.ok().put("data", new AddFriendRequestReplyTo(entity, sendUser, receiveUser));
    }

    /**
     * 拒绝加好友请求
     *
     * @param id
     * @return
     */
    @SaCheckLogin
    @GetMapping("/request/reject")
    public R disagreeAddRequest(@RequestParam("id") Integer id) {
        Long selfId = Long.parseLong((String) StpUtil.getLoginId());
        AddFriendRequestEntity entity = addFriendRequestService.disagreeAddFriendRuquest(id, selfId);
        // 更新UI
        R r1 = txUserFeignService.getUserById(selfId);
        UserTo receiveUser = r1.getData(new TypeReference<UserTo>() {
        });
        R r2 = txUserFeignService.getUserById(entity.getSendUserId());
        UserTo sendUser = r2.getData(new TypeReference<UserTo>() {
        });

        return entity != null ? R.ok().put("data", new AddFriendRequestReplyTo(entity, sendUser, receiveUser))
                : R.error(TxCodeEnume.COMMON_SYSTEM_UNKNOWN_EXCEPTION);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    @SaCheckLogin
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = addFriendRequestService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @SaCheckLogin
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        AddFriendRequestEntity addFriendRequest = addFriendRequestService.getById(id);

        return R.ok().put("addFriendRequest", addFriendRequest);
    }

    /**
     * 保存
     */
    @SaCheckLogin
    @RequestMapping("/save")
    public R save(@RequestBody AddFriendRequestEntity addFriendRequest) {
        addFriendRequestService.save(addFriendRequest);

        return R.ok();
    }

    /**
     * 修改
     */
    @SaCheckLogin
    @RequestMapping("/update")
    public R update(@RequestBody AddFriendRequestEntity addFriendRequest) {
        addFriendRequestService.updateById(addFriendRequest);

        return R.ok();
    }

    /**
     * 删除
     */
    @SaCheckLogin
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        addFriendRequestService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }




}
